package blog.service.impl;

import blog.dao.ConfigDao;
import blog.pojo.Config;
import blog.service.ConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    private ConfigDao configDao;

    @Override
    public String select(String name) throws Exception {
        return configDao.selectById(name).getValue();
    }

    @Override
    public Boolean update(String name, String value) throws Exception {
        return configDao.update(
                new Config().setValue(value),
                new QueryWrapper<>(new Config().setName(name))
        ) >0;
    }

    @Override
    public Boolean delete(String name) throws Exception {
        return configDao.delete(new QueryWrapper<>(new Config().setName(name))) > 0;
    }

    @Override
    public Boolean insert(String name, String value) throws Exception{
        Config dict = new Config()
                .setName(name)
                .setValue(value);
        return configDao.insert(dict) > 0;
    }

    public List<Config> selectAll() throws Exception{
        return configDao.selectList(null);
    }

}
